import { ref, computed } from 'vue'

export default function usePagination(queryData: any, conf: any) {
  const dataSource = ref<any>()
  const current = ref(1)
  const pageSize = ref(conf.pagination.pageSize || 10)
  const total = ref(0)
  const loading = ref(false)
  const pagination = computed(() => ({
    total: total.value,
    current: current.value,
    pageSize: pageSize.value
  }))

  async function run(
    dataConf: any = {
      current: current.value,
      pageSize: pageSize.value
    }
  ) {
    console.log('dataConf', dataConf)
    const res = await queryData({
      [conf.pagination.currentKey]: dataConf.current,
      [conf.pagination.pageSizeKey]: dataConf.pageSize
    })
    console.log('res-------', res)
    current.value = dataConf.current
    total.value = res.data.total
    dataSource.value = conf.formatResult(res)
  }
  run({ current: current.value, pageSize: pageSize.value })

  return {
    dataSource,
    pagination,
    loading,
    total,
    run,
    current,
    pageSize
  }
}
